﻿using Markdig;
using Markdig.Syntax;
using markdown_picture_packer.common.constants;
using markdown_picture_packer.common.enums;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace markdown_picture_packer.backend
{
    public class Markdown
    {
        //所在目录路径
        public readonly string FolderPath;
        //文件名(仅文件名,没有后缀)
        public readonly string FileName;
        //文件的上下文内容(每个元素代表一行)
        public readonly string[] Contents;
        //文件中图片
        public readonly List<Picture> Pictures;
        //补零后的名字长度
        public readonly int FillLength;


        public Markdown(string filePath)
        {
            FolderPath = Path.GetDirectoryName(filePath);
            FileName = Path.GetFileNameWithoutExtension(filePath);
            Contents = File.ReadAllLines(filePath);
            Pictures = new List<Picture>();
            List<Range> ranges = GetInvalidPictureFormatRanges();
            for (int index = 0; index < Contents.Length; index++)
            {
                if (Contents[index].Trim().Equals("")) continue;
                if (Range.InRanges(ranges, index)) continue;
                MarkdownPictureMatches(index);
                HTMLPictureMatches(index);
            }
            FillLength = $"{Pictures.Count}".Length + 1;
        }
        private void MarkdownPictureMatches(int index)
        {
            foreach (Match match in Constants.MD_PIC_PATTERN.Matches(Contents[index]))
            {
                //一种极端情况 一行出现两个一模一样的图片格式的内容,防止后续替换内容出现问题,我们要跳过多少行去找图片的索引
                if (Pictures.Count > 1 && Pictures[Pictures.Count - 1].Index == index && Pictures[Pictures.Count - 1].ShowType == PictureShowType.MARKDOWN && Pictures[Pictures.Count - 1].MatchedStr.Equals(match.Groups[0]))
                    Pictures.Add(new Picture(index, FolderPath, match.Groups[0].Value, match.Groups[1].Value, match.Groups[2].Value, PictureShowType.MARKDOWN, Pictures[Pictures.Count - 1].UrlRange.End));
                else
                    Pictures.Add(new Picture(index, FolderPath, match.Groups[0].Value, match.Groups[1].Value, match.Groups[2].Value, PictureShowType.MARKDOWN));
            }
        }
        private void HTMLPictureMatches(int index)
        {
            foreach (Match match in Constants.HTML_TAG_IMG_PATTERN.Matches(Contents[index]))
            {
                //一种极端情况 一行出现两个一模一样的图片格式的内容,防止后续替换内容出现问题,我们要跳过多少行去找图片的索引
                if (Pictures.Count > 1 && Pictures[Pictures.Count - 1].Index == index && Pictures[Pictures.Count - 1].ShowType == PictureShowType.HTML_TAG && Pictures[Pictures.Count - 1].MatchedStr.Equals(match.Groups[0]))
                    Pictures.Add(new Picture(index, FolderPath, match.Groups[0].Value, null, match.Groups[1].Value, PictureShowType.HTML_TAG, Pictures[Pictures.Count - 1].UrlRange.End));
                else
                    Pictures.Add(new Picture(index, FolderPath, match.Groups[0].Value, null, match.Groups[1].Value, PictureShowType.HTML_TAG));
            }
        }
        // 获取无效的图片格式的范围
        private List<Range> GetInvalidPictureFormatRanges()
        {
            string markdown = string.Join(Environment.NewLine, Contents);
            MarkdownPipeline pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
            MarkdownDocument document = Markdig.Markdown.Parse(markdown, pipeline);
            List<Range> invalidRanges = new List<Range>();
            for (int i = 0; i < document.Count; i++)
                if (document[i] is FencedCodeBlock) invalidRanges.Add(new Range(document[i].Line, i == document.Count - 2 ? Contents.Length - 1 : document[i + 1].Line));
            return invalidRanges;
        }
    }
}
